﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZDSoft.QLMS.Web.Helpers;
using ZDSoft.QLMS.Entity;
using NHibernate.Criterion;

namespace ZDSoft.QLMS.Web.Controllers
{
    public class KnowledgePointController : ControllerBase
    {
        public ActionResult Index()
        {
            IList<UserCourse> userCourses = AppHelper.LoginedUser.UserCourses;
            foreach (UserCourse entity in userCourses)
            {
                if (entity.IsCharge)
                {
                    TempData["CourseName"] = entity.Course.Name;
                }
            }
            return View();
        }

        public JsonResult GetAllKPByCourse()
        {
            Course course = new Course();
            IList<UserCourse> userCourses = AppHelper.LoginedUser.UserCourses;
            foreach (UserCourse entity in userCourses)
            {
                if (entity.IsCharge)
                {
                    course = entity.Course;
                }
            }
            IList<ICriterion> queryConditions = new List<ICriterion>();
            queryConditions.Add(Expression.Eq("Course", course));
            IList<KnowledgePoint> perList = new KnowledgePoint().Find(queryConditions);
            IList<KnowledgePoint> list = new List<KnowledgePoint>();
            foreach (KnowledgePoint entity in perList)
            {
                if (entity.Parent == null)
                {
                    list.Add(entity);
                }
            }
            IList<ZTreeHelper<KnowledgePoint>.ZTreeNode> tree = new List<ZTreeHelper<KnowledgePoint>.ZTreeNode>();
            foreach (KnowledgePoint entity in list)
            {
                if (entity.Parent == null)
                {
                    tree.Add(GetNodeDeatil(entity));
                }
            }
            return this.Json(tree, JsonRequestBehavior.AllowGet);
        }

        public ZTreeHelper<KnowledgePoint>.ZTreeNode GetNodeDeatil(KnowledgePoint entity)
        {
            ZTreeHelper<KnowledgePoint>.ZTreeNode node = new ZTreeHelper<KnowledgePoint>.ZTreeNode();
            if (entity.Children.Count == 0)
            {
                node = SetNode(entity);
            }
            else
            {
                node = SetNode(entity);
                node.children = new List<ZTreeHelper<KnowledgePoint>.ZTreeNode>();
                foreach (KnowledgePoint chi in entity.Children)
                {
                    node.children.Add(GetNodeDeatil(chi));
                }
            }
            return node;
        }
        public ZTreeHelper<KnowledgePoint>.ZTreeNode SetNode(KnowledgePoint entity)
        {
            ZTreeHelper<KnowledgePoint>.ZTreeNode node = new ZTreeHelper<KnowledgePoint>.ZTreeNode();
            node.id = entity.ID;
            node.tld = entity.IsActive.ToString();
            node.isParent = true;
            node.pid = 0;
            node.icon = "../../Content/img/diy/1_close.png";
            node.name = entity.Name;
            return node;
        }

        public string GetEntiyID(string name)
        {
            IList<ICriterion> queryConditions = new List<ICriterion>();
            queryConditions.Add(Expression.Eq("Name", name));
            IList<KnowledgePoint> list = new KnowledgePoint().Find(queryConditions);
            string result = list[0].ID.ToString();
            return result;
        }

        public void Delete(int id)
        {
            KnowledgePoint org = new KnowledgePoint().FindByID(id);
            if (org.Children.Count == 0)
            {
                new KnowledgePoint().Delete(id);
            }
            else
            {
                foreach (KnowledgePoint i in org.Children)
                {
                    Delete(i.ID);
                }
                new KnowledgePoint().Delete(id);
            }
        }

        public void Opreate(string isActive, string id, string name, string pId)
        {
            if (id == "0")
            {
                Course course = new Course();
                IList<UserCourse> userCourses = AppHelper.LoginedUser.UserCourses;
                foreach (UserCourse uc in userCourses)
                {
                    if (uc.IsCharge)
                    {
                        course = uc.Course;
                    }
                }
                KnowledgePoint entity = new KnowledgePoint();
                entity.Course = course;
                entity.Name = name;
                if (pId != "null")
                {
                    entity.Parent = new KnowledgePoint().FindByID(int.Parse(pId));
                }
                if (isActive != "1")
                {
                    entity.IsActive = false;
                }
                else
                {
                    entity.IsActive = true;
                }
                new KnowledgePoint().Create(entity);
            }
            else
            {
                KnowledgePoint entity = new KnowledgePoint().FindByID(int.Parse(id));
                entity.Name = name;
                if (isActive != "1")
                {
                    entity.IsActive = false;
                }
                else
                {
                    entity.IsActive = true;
                }
                new KnowledgePoint().Edit(entity);
            }
        }
    }
}
